<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css"/>
    <style>
        html, body {
            background-color: #f8f8f8;
        }
        .main ul li .li_box {
        	height: 95px;
        	background-color: #fff;
        	border-bottom: 1px solid #e8e8e8;
        	padding: 10px 15px 10px;
        	display: -webkit-box;
        	display: -webkit-flex;
        	position: relative
        }
        .main ul li:last-child .li_box {
        	border-bottom: none;
        }
        .main ul li .li_box .li_boxl .li_img {
        	width: 50px;
        	height: 50px;
        }
        .main ul li .li_box .li_boxl .li_img img {
        	width: 100%;
        	height: 100%;
        	object-fit: cover;
        }
        .main ul li .li_box .li_boxr {
        	width: 0;
			flex: 1;
			-webkit-flex: 1;
			box-flex: 1;
			-webkit-box-flex: 1;
			padding-left: 10px;
        }
        .main ul li .li_box .li_boxr .title_box {
        	display: -webkit-box;
        	display: -webkit-flex;
        	margin-bottom: 5px;
        }
        .main ul li .li_box .li_boxr .title_box .title {
        	flex: 1;
        	-webkit-flex: 1;
        	box-flex: 1;
        	-webkit-box-flex: 1;
        	padding-right: 10px;
        	overflow: hidden;
        	text-overflow: ellipsis;
        	white-space: nowrap;
        	font-size: 15px;
        	color: #333;
        }
        .main ul li .li_box .li_boxr .title_box .state {
        	font-size: 12px;
        	color: #999;
        }
        .main ul li .li_box .li_boxr .time {
			margin-bottom: 8px;
			font-size: 12px;
			color: #999;
			overflow: hidden;
        	text-overflow: ellipsis;
        	white-space: nowrap;
        }
        .main ul li .li_box .li_boxr .goods {
			margin-bottom: 8px;
			font-size: 12px;
			color: #999;
			overflow: hidden;
        	text-overflow: ellipsis;
        	white-space: nowrap;
        }
        .main ul li .li_box .li_boxr .price {
        	font-size: 16px;
        	color: #e62442;
        }
        .main ul li .li_box .li_boxr .btn_box {
            position: absolute;
            right: 15px;
            bottom: 7px;
            overflow: hidden;
        }
        .main ul li .li_box .li_boxr .btn_box .btn {
            width: 67px;
            height: 22px;
            line-height: 22px;
            float: left;
            text-align: center;
            background-color: #fff;
            border-radius: 5px;
            font-size: 12px;
            margin-right: 10px;
            border: 1px solid #999;
            color: #333;
        }
        .main ul li .li_box .li_boxr .btn_box .btn:last-child {
            margin-right: 0;
        }
        .main ul li .li_box .li_boxr .btn_box .btn_s {
            border: 1px solid #e62442;
            color: #e62442;
        }
        .loading{
            color: #ccc;
            text-align: center;
            line-height: 25px;
            display: none;
        }
        .without_data{
            display: none;
            color:#ccc;
            text-align:center;
            padding-top: 15px;
        }
        .hidden_img {
            display: none;
        }
    </style>
</head>
<body>
    <div class="main">
        <div class="without_data">暂无订单</div>
    	<ul id="order_ul">
    <!-- 		<li>
    			<div class="li_box">
    				<div class="li_boxl">
    					<div class="li_img">
    						<img src="../image/temp/1.png">
    					</div>
    				</div>
    				<div class="li_boxr">
    					<div class="title_box">
    						<div class="title">商家名称</div>
    						<div class="state">待付款</div>
    					</div>
    					<div class="time">2017-09-07 11:12:12</div>
    					<div class="goods">鱼香肉丝等3件商品</div>
    					<div class="price">RM29.5</div>
                        <div class="btn_box">
        					<div class="btn">继续支付</div>
                        </div>
    				</div>
    			</div>
    		</li> -->
    	</ul>
        <div class="loading">loading...</div>

    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/zepto.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript" src="../script/doT.min.js"></script>
<script id="orderTpl" type="text/x-dot-template">
    {{ for(var i=0;i<it.length;i++){ }}
        {{ if(it[i]['type'] == "0"){ }}
        <li onclick="fnOrderDetail({{=it[i]['id']}},{{=it[i]['status']}});" tapmode>
            <div class="li_box">
                <div class="li_boxl">
                    <div class="li_img">
                        <img src="../image/default.png">
                        <img onload="setImg(this);" class="hidden_img" src="{{=formatVideoImg(it[i]['shop_img'])}}">
                    </div>
                </div>
                <div class="li_boxr">
                    <div class="title_box">
                        <div class="title">{{=it[i]['shop_name']}}</div>
                        <div class="state">{{=it[i]['status_name']}}</div>
                    </div>
                    <div class="time">{{=it[i]['create_time']}}</div>
                    <div class="goods">{{=it[i]['goods_name']}}等{{=it[i]['goods_type_count']}}件商品</div>
                    <div class="price">RM{{=it[i]['must_pay']}}</div>
                    <div class="btn_box">
                        {{ if(it[i]['status'] == "0"){ }}
                            <div class="btn btn_s">继续支付</div>
                        {{ }; }}
                        {{ if(it[i]['status'] == "4"){ }}
                            <div class="btn" onclick="fnConfirmOrder({{=it[i]['id']}});" tapmode>确认收货</div>
                        {{ }; }}
                        {{ if(it[i]['status'] == "5"){ }}
                            <div class="btn btn_s" onclick="fnToComment({{=it[i]['id']}});" tapmode>去评论</div>
                        {{ }; }}
                        {{ if(it[i]['status'] == "6"){ }}
                            <div class="btn" onclick="fnOpenSeller({{=it[i]['shop_id']}});" tapmode>再来一单</div>
                        {{ }; }}
                        {{ if(it[i]['status'] == -2){ }}
                            <div class="btn" onclick="fnCheckRefund({{=it[i]['id']}});" tapmode>查看进度</div>
                        {{ }; }}
                    </div>
                </div>
            </div>
        </li>
        {{ } else{ }}
        <li onclick="fnPackageDetail({{=it[i]['id']}},{{=it[i]['status']}});" tapmode>
            <div class="li_box">
                <div class="li_boxl">
                    <div class="li_img">
                        <img style="background: #e6e6e6;" src="../image/package3.png">
                    </div>
                </div>
                <div class="li_boxr">
                    <div class="title_box">
                        <div class="title">包裹订单</div>
                        <div class="state">{{=it[i]['status_name']}}</div>
                    </div>
                    <div class="time">{{=it[i]['create_time']}}</div>
                    <div class="goods">包裹商品</div>
                    <div class="price">RM{{=it[i]['must_pay']}}</div>
                    <div class="btn_box">
                        {{ if(it[i]['status'] == "0"){ }}
                            <div class="btn btn_s">继续支付</div>
                        {{ }; }}
                        {{ if(it[i]['status'] == "5"){ }}
                        {{ }; }}
                        {{ if(it[i]['status'] == "6"){ }}
                        {{ }; }}
                    </div>
                </div>
            </div>
        </li>
        {{ }; }}
    {{ }; }}
</script>
<script type="text/javascript">
    apiready = function(){
        user = $api.getStorage('user_data');
        order_status = api.pageParam.order_status;
        p = 1;
        canLoad = true;
        //上拉加载
        api.addEventListener({
            name:'scrolltobottom',
            extra:{
                threshold:0            //设置距离底部多少距离时触发，默认值为0，数字类型
            }
        }, function(ret, err){
            if(canLoad) {
                p++;
                if($(window).scrollTop()) {
                    $(".loading").show();
                }
                fnGetOrder(2);
            }
        });

        //下拉刷新
        api.setCustomRefreshHeaderInfo({
            bgColor: '#f8f8f8',
            loadAnimInterval:60,
            image: {
               pull: pullArr,
               load: loadArr
            }
        }, function(ret, err) {
            p = 1;
            fnGetOrder(1);
        });

        //订单有了变化
        api.addEventListener({
            name: 'orderChangeEvent'
        }, function(ret, err) {
            if (ret) {
                p = 1;
                fnGetOrder(1);
            }
        });

        fnGetOrder();
    };

    function fnPackageDetail (id,status) {
        if(status >= 4){
            api.openWin({
                name: 'package_wait_confirm_and_finish',
                url: 'package_wait_confirm_and_finish.html',
                rect: {
                    x: 0,
                    y: 0,
                    w: 'auto',
                    h: 'auto'
                },
                pageParam:{
                    id:id,
                    status:status
                }
            });
        }else{
            api.openWin({
                name: 'package_order',
                url: 'package_order.html',
                rect: {
                    x: 0,
                    y: 0,
                    w: 'auto',
                    h: 'auto'
                },
                bounces: true,
                pageParam:{
                    id:id,
                    status:status
                }
            });
        }
        

        
    };

    function fnGetOrder(type) {
        if(canLoad == false) {
            return;
        }
        canLoad = false;
        api.ajax({
            url: rootPath+'/api/order/get_orders',
            method: 'get',
            cache: true,
            data: {
                values: { 
                    p:p,
                    uid : user.id,
                    token:user.token,
                    status: order_status,
                },
            }
        }, function(ret, err) {
            //type=1 下拉刷新，清除原来数据
            if(type == 1) {
                $('#order_ul').empty();
            }
            if (ret) {
                if(ret.status == 200) {
                    $('.without_data').hide();
                    $('.title_p').show();
                    appendDotData(ret.data,'orderTpl','order_ul',true);
                }else {
                    if(p == 1) {
                        $('.without_data').show();
                        $('.title_p').hide();
                    }
                }
            } else {
                showError(err.code,err.msg)
            }
            if(type == 1) {
                // 是下拉刷新的，要收回
                api.refreshHeaderLoadDone();
            }
            if(type == 2) {
                //上拉给点假时间,好看些         
                setTimeout(function(){
                    $(".loading").hide();
                },1000);
                setTimeout(function(){
                    canLoad = true;
                },1100)
            }else {
                $('.loading').hide();
                canLoad = true;
            }
            closeLoading();
        });
    }

    function fnOrderDetail(id,status) {
        if(status == 1 || status == 2 || status == 3 || status == 4) {
            api.openWin({
                name: 'order_detail2',
                url: './order_detail2.html',
                pageParam: {
                    id: id,
                    status : status
                }
            });
        }else{
            api.openWin({
                name: 'order_detail',
                url: './order_detail.html',
                pageParam: {
                    id: id,
                    status : status
                }
            });
        }
    }

    function fnConfirmOrder(order_id) {
        api.openWin({
            name: 'confirm_order',
            url: './confirm_order.html',
            pageParam: {
                id: order_id
            }
        });
        event.stopPropagation();
        return false;
    }

    function fnCheckRefund(order_id) {
        api.openWin({
            name: 'schedule',
            url: './schedule.html',
            pageParam: {
                id: order_id
            }
        });
        event.stopPropagation();
        return false;
    }

    function fnOpenSeller(shop_id) {
        api.openWin({
            name: 'seller',
            url: './seller.html',
            pageParam: {
                id: shop_id
            }
        });
    }

    function fnToComment(order_id) {
        api.openWin({
            name: 'release_evaluate_order',
            url: './release_evaluate_order.html',
            pageParam: {
                order_id: order_id
            }
        });
        event.stopPropagation();
        return false;
    }
</script>
</html>